/*
 * StartupWaitDialog2.java
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

/*
 * StartupWaitDialog2.java
 *
 * Created on Jan 27, 2009, 8:01:20 PM
 */

package vegadataeditor;

import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author lawinslow
 */
public class StartupWaitDialog2 extends javax.swing.JFrame {

    private Timer t;
    
    /** Creates new form StartupWaitDialog2 */
    public StartupWaitDialog2() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pleaseWaitLabel = new javax.swing.JLabel();
        foreverProgressBar = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(vegadataeditor.VegaDataEditorApp.class).getContext().getResourceMap(StartupWaitDialog2.class);
        pleaseWaitLabel.setText(resourceMap.getString("pleaseWaitLabel.text")); // NOI18N
        pleaseWaitLabel.setName("pleaseWaitLabel"); // NOI18N

        foreverProgressBar.setName("foreverProgressBar"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 171, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(foreverProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pleaseWaitLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 61, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pleaseWaitLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(foreverProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StartupWaitDialog2().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar foreverProgressBar;
    private javax.swing.JLabel pleaseWaitLabel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void setVisible(boolean b){
        if(b){
            startProgress();
        }else{
            stopProgress();
        }
        super.setVisible(b);
    }

    private void startProgress(){
        t = new Timer();
        t.schedule(new TimerTask(){
            private int progress = 0;
            @Override
            public void run() {
                progress++;
                update(getGraphics());
                if(progress > 100){
                    progress = 0;
                }
                foreverProgressBar.setValue(progress);
            }
        }, 0, 30);
    }

    private void stopProgress(){
        t.cancel();
        t = null;
    }
}
